package org.kymjs.aframe.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class KJPostParams extends KJParams {
    protected ConcurrentHashMap<String, FileBundle> fileParams;

    @Override
    protected void init() {
        super.init();
        fileParams = new ConcurrentHashMap<String, FileBundle>();
    }

    public KJPostParams() {
        init();
    }

    public KJPostParams(String key, String values) {
        this();
        put(key, values);
    }

    public KJPostParams(Map<String, Object> source) {
        this();
        for (Map.Entry<String, Object> entry : source.entrySet()) {
            if (entry.getValue() instanceof String) {
                urlParams.put(entry.getKey(), (String) entry.getValue());
            } else if (entry.getValue() instanceof InputStream) {
                put(entry.getKey(), (InputStream) entry.getValue());
            } else if (entry.getValue() instanceof File) {
                try {
                    put(entry.getKey(), (File) entry.getValue());
                } catch (FileNotFoundException e) {
                    throw new org.kymjs.aframe.exception.FileNotFoundException(
                            getClass().getName(), e);
                }
            }
        }
    }

    /**
     * 普通参数
     */
    public void put(String key, String value) {
        if (key != null && value != null) {
            urlParams.put(key, value);
        }
    }

    /**
     * 流参数
     */
    public void put(String key, InputStream stream) {
        put(key, stream, null);
    }

    /**
     * 文件参数
     */
    public void put(String key, File file) throws FileNotFoundException {
        if (key != null && file != null) {
            put(key, new FileInputStream(file), file.getName());
        }
    }

    public void put(String key, InputStream stream, String fileName) {
        put(key, stream, fileName, null);
    }

    /**
     * 添加 inputStream 到请求中.
     * 
     * @param stream
     *            要添加的输入流
     * @param fileName
     *            文件名
     * @param contentType
     *            文件的内容描述类型例如 application/json
     */
    public void put(String key, InputStream stream, String fileName,
            String contentType) {
        if (key != null && stream != null) {
            fileParams.put(key, new FileBundle(stream, fileName, contentType));
        }
    }

    /**
     * 移除key与对应的value
     */
    public void remove(String key) {
        if (key == null)
            return;
        urlParams.remove(key);
        fileParams.remove(key);
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        for (Map.Entry<String, String> entry : urlParams.entrySet()) {
            result.append(entry.getKey());
            result.append("=");
            result.append(entry.getValue());
            result.append("&");
        }

        for (Map.Entry<String, FileBundle> entry : fileParams.entrySet()) {
            result.append(entry.getKey());
            result.append("=");
            result.append(entry.getValue().inputStream);
            result.append("&");
        }
        return result.toString();
    }

    /**
     * 一个文件包裹
     * 
     * @author kymjs(kymjs123@gmail.com)
     */
    private static class FileBundle {
        public InputStream inputStream;
        public String fileName;
        public String contentType;

        public FileBundle(InputStream inputStream, String fileName,
                String contentType) {
            this.inputStream = inputStream;
            this.fileName = fileName;
            this.contentType = contentType;
        }

        public String getFileName() {
            if (fileName != null) {
                return fileName;
            } else {
                return "nofilename";
            }
        }
    }
}
